/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * inicioJPanel.java
 *
 * Created on 10-ago-2012, 17:44:54
 */
package Vista;

import Modelo.Clases.*;
import Modelo.*;
import java.util.Iterator;
import java.util.List;
import javax.swing.JOptionPane;
/**
 *
 * @author Home
 */
public class inicioJPanel extends javax.swing.JPanel {

    /** Creates new form inicioJPanel */
    public static Sesion sesion ;
    
    public inicioJPanel() {
        initComponents();
    }
    
     public inicioJPanel(int width , int height) {
        initComponents();
        this.setSize(width, height);
        this.setVisible(true);
        
        this.jEImagePanel1.setSize(width,height);
        
        
    }
     
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jEImagePanel1 = new recursos.JEImagePanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jPasswordField1 = new javax.swing.JPasswordField();
        jButton1 = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(940, 561));

        jEImagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/recursos/magic-wind-blue.jpg"))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Ravie", 0, 14));
        jLabel1.setText("Usuario :");

        jLabel2.setFont(new java.awt.Font("Ravie", 0, 14));
        jLabel2.setText("Clave :");

        jPasswordField1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jPasswordField1KeyTyped(evt);
            }
        });

        jButton1.setFont(new java.awt.Font("Ravie", 0, 11));
        jButton1.setText("Ingresar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jEImagePanel1Layout = new javax.swing.GroupLayout(jEImagePanel1);
        jEImagePanel1.setLayout(jEImagePanel1Layout);
        jEImagePanel1Layout.setHorizontalGroup(
            jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jEImagePanel1Layout.createSequentialGroup()
                .addGap(321, 321, 321)
                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPasswordField1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(344, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jEImagePanel1Layout.createSequentialGroup()
                .addGap(437, 437, 437)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
                .addGap(391, 391, 391))
        );
        jEImagePanel1Layout.setVerticalGroup(
            jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jEImagePanel1Layout.createSequentialGroup()
                .addGap(227, 227, 227)
                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(43, 43, 43)
                .addGroup(jEImagePanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jPasswordField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(54, 54, 54)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(162, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jEImagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jEImagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    
    String usuario = jTextField1.getText();
    char[] claveC = jPasswordField1.getPassword();
    String clave = ""; 
    for(int i = 0 ; i < claveC.length ; i++){   
        clave = clave + claveC[i];
    }
    boolean bandera = false ;
    List<Administrador> administradore = Modelo.consultarAdministradores();
   Iterator i = administradore.iterator();
   while(i.hasNext()){
       Administrador a = (Administrador)i.next();
       if(a.getUsuario().equals(usuario) && a.getClave().equals(clave)){      
        sesion = new Sesion(a);
        sesion.setEstaActiva(true);
        Principal.jButton1.setEnabled(true);
        Principal.jButton2.setEnabled(true);
        Principal.jButton3.setEnabled(true);
        Principal.jButton4.setEnabled(true);
        Principal.jButton5.setEnabled(true);
        Principal.jButton6.setEnabled(true);
        HistorialJPanel panel = new HistorialJPanel(940, 561);
        Principal.jPanel1.setVisible(false);
        Principal.jPanel1.removeAll();
        Principal.jPanel1.add(panel);
        Principal.jPanel1.setVisible(true);
        JOptionPane.showMessageDialog(null,"Bienvenido " + sesion.getAdministrador().getNombre());
        bandera = false ; 
        break;
       }else{
        bandera = true ;   
       }
   }
   if(bandera){
    JOptionPane.showMessageDialog(null,"Usuario o Clave incorrecto");
   }
    
}//GEN-LAST:event_jButton1ActionPerformed

private void jPasswordField1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jPasswordField1KeyTyped
// TODO add your handling code here:
    
    char  c = evt.getKeyChar();
     int p = c ;
         if ( p == 10)
    {   
       String usuario = jTextField1.getText();
    char[] claveC = jPasswordField1.getPassword();
    String clave = ""; 
    for(int i = 0 ; i < claveC.length ; i++){   
        clave = clave + claveC[i];
    }
    
    boolean bandera = false ;
    List<Administrador> administradore = Modelo.consultarAdministradores();
    
   Iterator i = administradore.iterator();
   
   while(i.hasNext()){
   
       Administrador a = (Administrador)i.next();
       
       if(a.getUsuario().equals(usuario) && a.getClave().equals(clave)){
             
        sesion = new Sesion(a);
        sesion.setEstaActiva(true);
           
       Principal.jButton1.setEnabled(true);
       Principal.jButton2.setEnabled(true);
       Principal.jButton3.setEnabled(true);
       Principal.jButton4.setEnabled(true);
       Principal.jButton5.setEnabled(true);
       Principal.jButton6.setEnabled(true);
       /**********************/
        HistorialJPanel panel = new HistorialJPanel(940, 561);
        Principal.jPanel1.setVisible(false);
        Principal.jPanel1.removeAll();
    
        Principal.jPanel1.add(panel);
        Principal.jPanel1.setVisible(true);
     /********************************/  
       JOptionPane.showMessageDialog(null,"Bienvenido " + sesion.getAdministrador().getNombre());
          bandera = false ; 
          break;
       }else{
        bandera = true ;   
       }
   }
   
   if(bandera){
    JOptionPane.showMessageDialog(null,"Usuario o Clave incorrecto");
   }
        
    }else{
    
    }
}//GEN-LAST:event_jPasswordField1KeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private recursos.JEImagePanel jEImagePanel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField jPasswordField1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
